/*
 * Copyright (c) 2021, 2023 Oracle and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.helidon.integrations.common.rest;

import io.helidon.http.Headers;
import io.helidon.http.Status;

/**
 * A base for responses without an entity.
 * For responses that may have an entity, use {@link io.helidon.integrations.common.rest.ApiOptionalResponse},
 * for responses that must have an entity, use {@link io.helidon.integrations.common.rest.ApiEntityResponse}.
 */
public abstract class ApiResponse extends ApiJsonParser {
    private final Headers headers;
    private final Status status;
    private final String requestId;

    /**
     * Create a new rest response.
     *
     * @param builder subclass of builder
     */
    protected ApiResponse(Builder<?, ?> builder) {
        this.headers = builder.headers;
        this.status = builder.status;
        this.requestId = builder.requestId;
    }

    /**
     * HTTP headers returned in the response.
     *
     * @return HTTP headers
     */
    public Headers headers() {
        return headers;
    }

    /**
     * HTTP status returned. In most cases for successful invocation, this should be
     * one of statuses in the {@code 200} family.
     * In case the invocation resulted in an error, an exception is thrown instead of returning a
     * response.
     *
     * @return status of the response (only if successful)
     */
    public Status status() {
        return status;
    }

    /**
     * Request ID that was used. As this ID may be generated during request, this is the only
     * way to obtain it.
     *
     * @return request ID used when invoking the integrated system
     */
    public String requestId() {
        return requestId;
    }

    /**
     * Abstract fluent API builder to build subclasses of {@link ApiResponse}.
     *
     * @param <B> type of the builder
     * @param <T> type of api response
     */
    public abstract static class Builder<B extends Builder<B, T>, T extends ApiResponse>
            implements io.helidon.common.Builder<B, T> {
        private Headers headers;
        private Status status;
        private String requestId;

        /**
         * New builder.
         */
        protected Builder() {
        }

        /**
         * Configure the HTTP headers returned by the API call.
         *
         * @param headers headers
         * @return updated builder
         */
        public B headers(Headers headers) {
            this.headers = headers;
            return me();
        }

        /**
         * Response status returned by the API call.
         *
         * @param status HTTP status
         * @return updated builder
         */
        public B status(Status status) {
            this.status = status;
            return me();
        }

        /**
         * Request ID used when dispatching this request.
         *
         * @param requestId request id
         * @return updated builder
         */
        public B requestId(String requestId) {
            this.requestId = requestId;
            return me();
        }

        /**
         * Received HTTP headers.
         *
         * @return headers
         */
        public Headers headers() {
            return headers;
        }

        /**
         * Received HTTP status.
         *
         * @return status
         */
        public Status status() {
            return status;
        }

        /**
         * Request ID used to invoke the request.
         * This may be generated by {@link io.helidon.integrations.common.rest.RestApi}
         * @return request id
         */
        public String requestId() {
            return requestId;
        }

        /**
         * Can be returned by subclasses that can be subclassed again.
         *
         * @return this instance as a subclass type
         */
        @SuppressWarnings("unchecked")
        protected B me() {
            return (B) this;
        }
    }
}
